<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_app_func('global');
class tools extends admin {
	private $db;

	function __construct() {
		parent::__construct();
		pc_base::load_sys_class('form');
		pc_base::load_app_class('Fusion_Image');
		$this->member = pc_base::load_model('member_model');
		$this->order = pc_base::load_model('order_info_model');
		$this->brand = pc_base::load_model('brand_model');
		$this->stats = pc_base::load_model('ushop_stats_model');
		$this->info = pc_base::load_model('order_info_model');
		$this->goods = pc_base::load_model('order_goods_model');
	}
	
	function init(){
		//订单状态
		$order_status = array('待确认','待付款','待发贷','已完成','付款中','取消','无效','退贷');
		include $this->admin_tpl('order_list');
	}	
	
	//客户统计
	public function member_stats() {
		$user_num = count($this->member->listinfo());
		//有过订单的会员数 
		$sql = "SELECT COUNT(DISTINCT user_id) FROM `v9_ushop_order_info` WHERE user_id > 0 GROUP BY user_id";
		$total_fee = $this->order->query($sql);
		while($value = mysql_fetch_array($total_fee)) {
			$total_fee = $value['COUNT(DISTINCT user_id)'];
		}
	  	//会员订单总数和订单总购物额
		$sql = "SELECT COUNT(*) AS order_num, SUM( theTotalAmount ) AS turnover FROM `v9_ushop_order_info` WHERE user_id > 0 AND order_status IN ('1','5') AND shipping_status IN ('1','2') AND pay_status IN ('2','1')";
		$user_all_order = $this->order->query($sql);
		while($value = mysql_fetch_array($user_all_order)) {
			$user_order_num = floatval($value['order_num']);
			$user_turnover = floatval($value['turnover']);
		}
		include $this->admin_tpl('user_orderby');	
	}
	/**
		订单统计
	*/
	public function order_stats() {
		$libpath = "./phpcms/modules/ushop/lib";
		
		/* 未确认订单数 */
		$where =  " order_status = '0' ";
		$order_info['unconfirmed_num'] = count($this->order->listinfo($where));
		/* 已确认订单数 */
		$where = " order_status = '1'";
		$order_info['confirmed_num'] = count($this->order->listinfo($where));
		/* 已成交订单数 */
		$where = " order_status IN ('1','5') AND shipping_status IN ('1','2') AND pay_status IN ('2','1') ";
		$order_info['succeed_num'] = count($this->order->listinfo($where));
		/* 无效或已取消订单数 */
		$where = " order_status > '1' ";
		$order_info['invalid_num'] = count($this->order->listinfo($where));
		//订单概况的数据
		$data_array = array('已确认' => $order_info['confirmed_num'], '已成交' => $order_info['succeed_num'], '未确认' => $order_info['unconfirmed_num'], '无效或已取消' => $order_info['invalid_num']);
		$Fusion_Image0 = new Fusion_Image('订单概况', $data_array);
		$Fusion_Images0 =  $Fusion_Image0->renderChartHTML($libpath.'/FC2Column.so', "", "800", "600");
		//支付方式的数据
		
		$pay_online = count($this->order->listinfo(array('pay_style'=>'0')));
		$pay_company = count($this->order->listinfo(array('pay_style'=>'1')));
		$pay_post = count($this->order->listinfo(array('pay_style'=>'2')));
		
		$pay_style = array('在线支付'=>$pay_online,'公司转账'=>$pay_company,'邮局汇款'=>$pay_post);
		$Fusion_Image2 = new Fusion_Image('支付方式', $pay_style);
		$Fusion_Images2 =  $Fusion_Image2->renderChartHTML($libpath.'/FC2Column.so', "", "800", "600");
		include $this->admin_tpl('order_stats');
	}
	//销售排行
	public function sale_general() {
		extract($_POST['search']);
		$start_date = $start_time ? strtotime($start_time) : strtotime('-7 days');
		$end_time = $end_time ? strtotime($end_time) : time();

		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$where = " order_status IN ('1','5') AND shipping_status IN ('1','2') AND shipping_time >= '$start_date' AND shipping_time < ".( $end_time + 86400);
		$infos = $this->info->listinfo($where);
		foreach($infos as $key => $value){
			foreach(unserialize($value['goods_id']) as $k => $v){
				$new_array = $this->count_goods(unserialize($value['goods_id']),$v['goodsid']);
			}
		}
		$pages = $this->info->pages;
		uasort($new_array,$this->sortByOrder);
		include $this->admin_tpl('sale_general');
	}
	//销售明细
	public function sale_list() {
		extract($_POST['search']);
		$start_date = $start_time ? strtotime($start_time) : strtotime('-7 days');
		$end_time = $end_time ? strtotime($end_time) : time();
		$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "order_status IN ('1','5') AND shipping_status IN ('1','2') AND shipping_time >= '$start_date' AND shipping_time < ".( $end_time + 86400);
		$infos = $this->info->listinfo($where);
		
		foreach($infos as $key => $value) {
			foreach (unserialize($value['goods_id']) as $k => $v) {
				$v['shipping_time'] = $value['shipping_time'];
				$v['order_sn'] = $value['order_sn'];
				$more_order[] = $v;
			}
		}
		$pages = $this->info->pages;
		include $this->admin_tpl('sale_list');
	}
	
	private function count_goods($array,$id) {
		$new_array = array();
		foreach($array as $key => $value) {
			if($value['goodsid'] == $id){
				if(array_key_exists($id,$new_array)) {
					$new_array[$id] = array('price'=>$new_array[$id]['price'] + $value['price'],'i'=>$new_array[$id]['i']+1);   
				}else{
					$new_array[$id] = array('price'=>$value['price'],'i'=>$id,'title'=>$value['title']);   
				}
			}
		}
		return $new_array;
	}

	private function sortByOrder($x,$y) {
		if($x == $y) return 0;
		return $x['i'] > $y['i'] ? 1 : -1;
	}
}
?>